﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class NhaDatRepository
	{
		public List<NhaDat> SelectAll()
		{
			return DataContext.Instance.NhaDats.ToList();
		}

		public NhaDat SelectByID(int id)
		{
			return DataContext.Instance.NhaDats.FirstOrDefault(item => item.ID == id );
		}

		public List<NhaDat> SelectPaging(int page, int pageSize)
		{
			var lstItem = DataContext.Instance.NhaDats.ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(NhaDat obj)
		{
			try
			{
				DataContext.Instance.NhaDats.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.NhaDats.FirstOrDefault(item => item.ID == id );
                if (delitem.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.NhaDatLe))
                {
                    if (delitem.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.CanHo))
                    {
                        var tienich = delitem.TienIch;
                        if (tienich == null)
                        {
                            tienich = DataContext.Instance.TienIches.FirstOrDefault(item => item.IDNhaDat == id);
                            DataContext.Instance.TienIches.DeleteOnSubmit(tienich);
                        }
                        else
                            DataContext.Instance.TienIches.DeleteOnSubmit(delitem.TienIch);

                        var tienichkhac = delitem.TienIchKhac;
                        if (tienichkhac == null)
                        {
                            tienichkhac = DataContext.Instance.TienIchKhacs.FirstOrDefault(item => item.IDNhaDat == id);
                            DataContext.Instance.TienIchKhacs.DeleteOnSubmit(tienichkhac);
                        }
                        else
                            DataContext.Instance.TienIchKhacs.DeleteOnSubmit(delitem.TienIchKhac);

                        if (delitem.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.CanHo))
                        {
                            var thongtincanhole = delitem.ThongTinCanHoLe;
                            if (thongtincanhole == null)
                            {
                                thongtincanhole = DataContext.Instance.ThongTinCanHoLes.FirstOrDefault(item => item.IDNhaDat == id);
                                DataContext.Instance.ThongTinCanHoLes.DeleteOnSubmit(thongtincanhole);
                            }
                            else
                                DataContext.Instance.ThongTinCanHoLes.DeleteOnSubmit(delitem.ThongTinCanHoLe);
                        }
                    }

                    var vhxh = delitem.VanHoaXaHoi;
                    if (vhxh == null)
                    {
                        vhxh = DataContext.Instance.VanHoaXaHois.FirstOrDefault(item => item.IDNhaDat == id);
                        if (vhxh != null)
                            DataContext.Instance.VanHoaXaHois.DeleteOnSubmit(vhxh);
                    }
                    else
                        DataContext.Instance.VanHoaXaHois.DeleteOnSubmit(delitem.VanHoaXaHoi);
                }
				DataContext.Instance.NhaDats.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
                throw new Exception(ex.Message);
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.NhaDats.Count();
		}

		public int GetTotalItemBy(int id)
		{
			return (from p in DataContext.Instance.NhaDats where p.ID == id select p).Count();
		}

		public int GetTotalItemByLoaiBatDongSan(int idloaibatdongsan)
		{
			return (from p in DataContext.Instance.NhaDats where p.IDLoaiBatDongSan == idloaibatdongsan select p).Count();
		}

		public int GetTotalItemByQuanHuyen(int idquanhuyen)
		{
			return (from p in DataContext.Instance.NhaDats where p.IDQuanHuyen == idquanhuyen select p).Count();
		}

        public int GetTotalItemByLoaiBatDongSanQuanHuyen(int idloaibatdongsan, int idquanhuyen)
        {
            return (from p in DataContext.Instance.NhaDats where p.IDLoaiBatDongSan == idloaibatdongsan && p.IDQuanHuyen == idquanhuyen select p).Count();
        }

		public int GetTotalItemByLoaiGia(int idloaigia)
		{
			return (from p in DataContext.Instance.NhaDats where p.IDLoaiGia == idloaigia select p).Count();
		}

		public int GetTotalItemByNguoiDang(Guid idnguoidang)
		{
			return (from p in DataContext.Instance.NhaDats where p.IDNguoiDang == idnguoidang select p).Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageBy(int id)
		{
			int RecordCount = GetTotalItemBy(id);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByLoaiBatDongSan(int idloaibatdongsan)
		{
			int RecordCount = GetTotalItemByLoaiBatDongSan(idloaibatdongsan);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByQuanHuyen(int idquanhuyen)
		{
			int RecordCount = GetTotalItemByQuanHuyen(idquanhuyen);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByLoaiGia(int idloaigia)
		{
			int RecordCount = GetTotalItemByLoaiGia(idloaigia);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByNguoiDang(Guid idnguoidang)
		{
			int RecordCount = GetTotalItemByNguoiDang(idnguoidang);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<NhaDat> RetrieveByID(int id)
		{
            var items = (from item in DataContext.Instance.NhaDats where item.ID == id select item).ToList();
            return items;
		}

        public void UpdateSoLanXem(int id)
        {
            var nhadat = (from item in DataContext.Instance.NhaDats where item.ID == id select item).ToList()[0];
            nhadat.SoLanXem = nhadat.SoLanXem + 1;
            Save();
        }
		public List<NhaDat> SelectByLoaiBatDongSan(int idloaibatdongsan, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.NhaDats where item.IDLoaiBatDongSan == idloaibatdongsan select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public List<NhaDat> SelectByQuanHuyen(int idquanhuyen, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.NhaDats where item.IDQuanHuyen == idquanhuyen select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

        public List<NhaDat> SelectByLoaiBatDongSanQuanHuyen(int idloaibatdongsan, int idquanhuyen, int page, int pageSize)
        {
            var lstItem = (from item in DataContext.Instance.NhaDats where item.IDLoaiBatDongSan == idloaibatdongsan && item.IDQuanHuyen == idquanhuyen select item).ToList();
            if (pageSize > 0)
            {
                int startIndex, itemCount;
                startIndex = (page - 1) * pageSize;
                itemCount = pageSize;
                if (startIndex + itemCount > lstItem.Count)
                    itemCount = lstItem.Count - startIndex;
                lstItem = lstItem.GetRange(startIndex, itemCount);
            }
            return lstItem;
        }

		public List<NhaDat> SelectByLoaiGia(int idloaigia, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.NhaDats where item.IDLoaiGia == idloaigia select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public List<NhaDat> SelectByNguoiDang(Guid idnguoidang, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.NhaDats where item.IDNguoiDang == idnguoidang select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

        public List<NhaDat> SelectDatNen()
        {
            return (from item in DataContext.Instance.NhaDats.OrderByDescending(item => item.NgayDang)
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.DatNen)
                    select item).ToList();
        }

        public List<NhaDat> SelectCanHo()
        {
            return (from item in DataContext.Instance.NhaDats.OrderByDescending(item => item.NgayDang)
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.CanHo) ||
                        item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.SanPham) ||
                        item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.Nha)
                    select item).ToList();
        }

        public List<NhaDat> SelectDuAnMoiNhat(int pageSize)
        {
            var lst = (from item in DataContext.Instance.NhaDats.OrderByDescending(item=>item.NgayDang) 
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.NhaDatDuAn) && item.TieuBieu == false && item.TinVip == false
                    select item).Take(pageSize).ToList();
            return lst;
        }

        public List<NhaDat> SelectDuAnTieuBieu(int pageSize)
        {
            return (from item in DataContext.Instance.NhaDats.OrderByDescending(item => item.NgayDang)
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.NhaDatDuAn) && 
                    item.TieuBieu == true 
                    select item).Take(pageSize).ToList();
        }

        public List<NhaDat> SelectDuAnVip(int pageSize)
        {
            return (from item in DataContext.Instance.NhaDats.OrderByDescending(item => item.NgayDang)
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.NhaDatDuAn) &&
                    item.TinVip == true
                    select item).Take(pageSize).ToList();
        }

        public List<NhaDat> SelectSanPhamLeTieuBieu(int pageSize)
        {
            return (from item in DataContext.Instance.NhaDats.OrderByDescending(item => item.NgayDang)
                    where item.LoaiBatDongSan.TenLoaiBatDongSan.ToUpper().Contains(WebConfiguration.NhaDatLe) && 
                    item.TieuBieu == true 
                    select item).ToList();
        } 
	}
}